<%--
  Created by IntelliJ IDEA.
  User: CMCC
  Date: 2016/8/30
  Time: 16:22
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <link rel="stylesheet" href="css/bootstrap.min.css"/>
    <link rel="stylesheet" href="css/contractInfo.css"/>
    <link rel="stylesheet" href="css/pagination.css"/>
    <script type="text/javascript" src="js/jquery-3.1.0.min.js"></script>
    <script type="text/javascript" src="layer/layer.js"></script>
    <script type="text/javascript" src="js/handlebars-v3.0.3.js"></script>
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/mui.min.js"></script>
    <script type="text/javascript" src="js/security.min.js"></script>
    <script type="text/javascript" src="js/pagination.js"></script>
    <script type="text/javascript">
        var hosturl = "http://"+ window.location.host + "/"+ window.location.pathname.split('/')[1];
        var adminSessionID = <%=session.getAttribute("SID")%>;
        var adminSessionName = "<%=session.getAttribute("SName")%>";
        if (adminSessionID === null) {
            window.location.href = hosturl+"/login.jsp";
        } else {
        }
    </script>
    <script type="text/javascript">
        window.onload = function () {
            var oUserInfo = document.getElementById('userInfo');
            var ocCntractInfo = document.getElementById('contractInfo');
            var oLogInfo = document.getElementById('logInfo');
            document.getElementById("adminName").innerHTML = " " + adminSessionName + " ";

            oUserInfo.onmousedown = function () {
                ocCntractInfo.style.backgroundColor = "#fff";
                ocCntractInfo.style.color = "#000";
                oUserInfo.style.backgroundColor = "#324e75";
                oUserInfo.style.color = "#fff";
            };
            ocCntractInfo.onmousedown = function () {
                oUserInfo.style.backgroundColor = "#fff";
                oUserInfo.style.color = "#000";
                ocCntractInfo.style.backgroundColor = "#324e75";
                ocCntractInfo.style.color = "#fff";
            }
            oUserInfo.onclick = function () {
                window.location.href = hosturl+'/userMana.jsp';
            };
            oLogInfo.onclick = function () {
                window.location.href = hosturl+'/logMana.jsp';
            };
            //添加合同信息

            //注销
            var oLogout = document.getElementById('logout');
            oLogout.onclick = function () {
                mui.ajax(hosturl+"/admin/logout", {
                    data: {
//                    "contractId": 1
                    },
                    dataType: "json",
                    type: "get",
                    timeout: 10000,
                    success: function(data) {
                        alert(data.description)
                        window.location.href = 'login.jsp';

                    },
                    error: function(xhr, type, errorThrown) {
                        console.log(type);
                    }
                });

            };

        }
    </script>
    <script type="text/javascript">
        mui.ready(function () {
            mui.ajax(hosturl+"/contract/byAdmin", {
                data: {
//                    "contractId": 1
                },
                dataType: "json",
                type: "post",
                timeout: 10000,
                success: function(data) {
                    document.getElementById("dataLen").innerHTML="共有"+data.body.length +"条";
                    $('#pagination-container').pagination({
                        dataSource: data.body,
                        pageSize: 5,
                        showGoInput: true,
                        showGoButton: true,
                        autoHidePrevious: true,
                        autoHideNext: true,
//                        showPrevious: false,
//                        showNext: false,
//                        showPageNumbers: false,
//                        showNavigator: true,
                        pageNumber: 1,
                        callback: function (data, pagination) {
                            // template method of yourself
                            var html = simpleTemplating(data);
                            $('#data-container').html(html);
                        }
                    });

                },
                error: function (xhr, type, errorThrown) {
                    console.log(type);
                }
            });
        });
    </script>
    <title>合同信息</title>
</head>

<body>
<div class="top">
    <div class="top-left">
        <p class="top-left">合同管理系统</p>
    </div>
    <div class="top-right">

        <p class="p1">欢迎管理员<span class="" id="adminName"> 张三 </span>的到来 <a href="#" id="logout">注销 </a></p>
    </div>
</div>
<div class="left-block">
    <ul>
        <li id="contractInfo" class="cleft-index">合同管理</li>
        <li id="userInfo" class="uleft-index">用户管理</li>
        <li id="logInfo" class="uleft-index">日志管理</li>
    </ul>
</div>
<div class="main-part">
    <ul>
        <li class="right-label">合同信息</li>
        <!--<li class=""><input class="right-btn" type="button" value="修改合同信息" /></li>-->
        <!--<li class=""><input class="right-btn" type="button" value="搜索" /></li>-->
        <li class=""><a href="/file/download?fileName=ContractTemplet.xls"><input class="right-btn" type="button" id="downLoadBtn2"
                                                          value="下载合同模板"/></a></li>


        <li class=""><a href="javascript:load();"><input class="right-btn" type="button" id="loadBt" value="批量导入合同"/></a>
        </li>
        <li class=""><a href="javascript:add();"><input class="right-btn" type="button" id="addBtn" value="添加合同信息"/></a>
        </li>
        <li class="full-width"></li>
        <li class="">
            <div class="row">
                <div class="col-lg-4">
                    <div class="input-group">
                        <span class="input-group-addon" id="basic-addon1">合同编号</span>
                        <input id="contractId" type="text" class="form-control" placeholder="合同编号"
                               aria-describedby="basic-addon1">
                    </div>
                </div>
                <div class="col-lg-4">
                    <div class="input-group">
                        <span class="input-group-addon" id="basic-addon2">合同名称</span>
                        <input id="contractName" type="text" class="form-control" placeholder="合同名称"
                               aria-describedby="basic-addon1">
                    </div>
                </div>

                <div class="col-lg-4">
                    <div class="input-group">
                        <span class="input-group-addon" id="basic-addon5">用户名</span>
                        <input id="userName" type="text" class="form-control" placeholder="用户名"
                               aria-describedby="basic-addon1">
                    </div>
                </div>
                <div class="col-lg-4">
                    <div class="input-group">
                        <span class="input-group-addon" id="basic-addon3">开始时间</span>
                        <input id="startTime" type="date" class="form-control" placeholder=""
                               aria-describedby="basic-addon1">
                    </div>
                </div>
                <div class="col-lg-4">
                    <div class="input-group">
                        <span class="input-group-addon" id="basic-addon4">结束时间</span>
                        <input id="endTime" type="date" class="form-control" placeholder=""
                               aria-describedby="basic-addon1">
                    </div>
                </div>
                <div class="col-lg-4">
                    <button id="search" onclick="searchContract()" type="button" class="btn btn-primary">搜索</button>
                    <label id="dataLen">共有？条</label>
                    <input id="handle_status" value="" hidden="hidden">
                </div>
            </div>
        </li>
        <li>
            <table id="table1" class="table table-bordered table-striped table-hover">
                <thead>
                <tr>
                    <th>序号</th>
                     <th>合同编号</th>
                    <th>合同名称</th>
					<%--<th>用户编号</th>--%>
                    <th>用户名</th>
                    <th>付款期数</th>


                    <th>开始时间</th>
                    <th>结束时间</th>
                    <th>是否提醒</th>
                    <th>提醒时间</th>


                    <th>操作</th>
                </tr>
                </thead>

                <tbody id="data-container">
                <%--<div id="data-container"></div>--%>
                </tbody>

            </table>
            <div id="wrapper">
                <section>
                    <div id="pagination-container"></div>
                </section>
            </div>
        </li>
    </ul>
</div>
</body>
</html>
<script type="text/javascript">
    //处理分页数据
    function simpleTemplating(data) {
        var html = '';
        $.each(data, function (index, item) {
            var warnOrNor = (item[0].warnOrNor==false)?"否":"是";
            var remindTime = (item[0].remind==null)?"":item[0].remind;
            html += '<tr>';
            html += '<td>'+ item[0].id +'</td>';
            html += '<td>'+ item[0].contractId +'</td>';
            html += '<td>'+ item[0].contractName +'</td>';
            html += '<td>'+ item[1].userName +'</td>';
            html += '<td><a href="javascript:payRemind(' + item[0].id + ','+ item[0].times +');">' + item[0].times + '</a></td>';
            html += '<td>'+ item[0].start +'</td>';
            html += '<td>'+ item[0].deadline +'</td>';
            html += '<td>'+ warnOrNor +'</td>';
            html += '<td>'+ remindTime + '</td>';
            html +=  '<td><a href="javascript:update(' + item[0].id  + ');">修改</a>&nbsp;&nbsp;&nbsp;<a href="javascript:del(' + item[0].id + ',' + item[0].contractId + ');">删除</a></td>';
            html += '</tr>';
        });
        return html;
    };
    
    function del(con_id,contractId){
        if (confirm("确定要删除编号为" + contractId + "的合同吗？")) {
        var id = con_id + "";
        mui.ajax(hosturl+"/contract/deleteContract", {
            data: {
                "contractId" : id
            },
            dataType: "json",
            type: "post",
            timeout: 10000,
            success: function(datas) {
                if(datas.status=="ok"){
                    window.location.reload();
                }else{
                    alert(datas.description);
                }
            },
            error: function(xhr, type, errorThrown) {
                console.log(type);
            }
        });
        };
    };

    function add() {
        layer.open({
            type: 2,
            title: '添加合同信息',
            shadeClose: true,
            shade: [0.3, '#000'],
            area: ['543px', '543px'],
            content: [hosturl+'/addContractByAdmin.html','no'],
            end: function () {
                var handle_status = $("#handle_status").val();
                if ( handle_status == 'OK' ) {
                    layer.msg('添加合同信息成功！',{
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                    setTimeout(searchContract(),2000);
                } else if ( handle_status == 'none' ) {
                    layer.msg('添加合同信息失败！',{
                        icon: 2,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                    window.location.reload();
                }
            }
        });
        layer.iframeAuto();
    };


    function load() {
        layer.open({
            type: 2,
            title: '批量导入合同',
            shadeClose: true,
            shade: [0.3, '#000'],
            area: ['543px', '543px'],
            content: hosturl+'/addContractByExcel.html',
            end: function () {
                var handle_status = $("#handle_status").val();
                if ( handle_status == 'OK' ) {
                    layer.msg('导入合同信息成功！',{
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                    setTimeout(searchContract(),2000);
                } else if ( handle_status == 'none' ) {
                    layer.msg('您没有导入任何合同信息！',{
                        icon: 2,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                    window.location.reload();
                }
            }
        });
    };


    function update(id) {
        layer.open({
            type: 2,
            title: '修改合同信息',
            shadeClose: true,
            shade: [0.3, '#000'],
            area: ['543px', '543px'],
            content: [hosturl+'/updateContract.html?id='+id,'no'],
            end: function () {
                var handle_status = $("#handle_status").val();
                if ( handle_status == 'OK' ) {
                    layer.msg('合同信息修改成功！',{
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                    setTimeout(searchContract(),2000);
                } else if ( handle_status == 'none' ) {
                    layer.msg('合同信息修改失败！',{
                        icon: 2,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    });
                    window.location.reload();
                }
            }
        });
        layer.iframeAuto();
    };
    
    function payRemind(id,num) {
        layer.open({
            type: 2,
            title: '分期付款时间',
            shadeClose: true,
            shade: [0.3, '#000'],
            area: ['600px', '500px'],
            content: [hosturl+'/payRemindMana.html?id=' + id + "&num=" + num,'no'],
            end: function () {
//                var handle_status = $("#handle_status").val();
//                if ( handle_status == 'OK' ) {
//                    layer.msg('合同修改成功！',{
//                        icon: 1,
//                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
//                    });
//                    setTimeout(searchContract(),2000);
//                } else if ( handle_status == 'none' ) {
//                    layer.msg('合同信息修改失败！',{
//                        icon: 2,
//                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
//                    });
//                    window.location.reload();
//                }
//                window.location.reload();
                
            }
        });
        layer.iframeAuto();
    };
    
    function logout() {
        mui.ajax(hosturl+"/user/logout", {
            data: {},
            dataType: "json",
            type: "get",
            timeout: 10000,
            success: function (datas) {
                if (datas.status == "ok") {
                    window.location.href = 'login.jsp';

                    alert(datas.description);
                }
            },

            error: function (xhr, type, errorThrown) {
                console.log(type);
            }
        });
    };

    function searchContract() {
        var contractId = $("#contractId").val();
        var contractName = $("#contractName").val();
        var userName = $("#userName").val();
        var start = $('#startTime').val();
        var deadline = $('#endTime').val();

        mui.ajax(hosturl+"/contract/searchReqByAdmin", {
            data: {
                'contractId': contractId,
                'contractName': contractName,
                'userName': userName,
                'start': start,
                'deadline': deadline
            },
            dataType: "json",
            type: "post",
            timeout: 10000,
            success: function (data) {

                if (data.status == "datanull") {
                    $('#data-container').html("");
                    $('#pagination-container').html("");
                    document.getElementById("dataLen").innerHTML="共有0条";
                } else {
                    document.getElementById("dataLen").innerHTML="共有"+data.body.length +"条";

                    $('#pagination-container').pagination({
                        dataSource: data.body,
                        pageSize: 5,
                        showGoInput: true,
                        showGoButton: true,
                        autoHidePrevious: true,
                        autoHideNext: true,
//                        showPrevious: false,
//                        showNext: false,
//                        showPageNumbers: false,
//                        showNavigator: true,
                        pageNumber: 1,
                        callback: function (data, pagination) {
                            // template method of yourself
                            var html = simpleTemplating(data);
                            $('#data-container').html(html);
                        }
                    });
                }

            },
            error: function (xhr, type, errorThrown) {
                console.log(type);
            }
        });
    };
</script>

